using System;
using System.Collections.Generic;
using System.Linq;
using ServiceMReviewer.Model;
using ServiceMReviewer.Helper;

namespace ServiceMReviewer.Repositories
{
	public class FoodTypeRepository
	{
		public List<FoodType> SelectAll()
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				return ctx.FoodTypes.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<FoodType>();
		}

		public FoodType SelectByID(int id)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				return ctx.FoodTypes.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(FoodType obj)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				ctx.FoodTypes.InsertOnSubmit(obj);
				ctx.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(int id)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				var delitem = ctx.FoodTypes.FirstOrDefault(item => item.ID == id );
				ctx.FoodTypes.InsertOnSubmit(delitem);
				ctx.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				ctx.SubmitChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<FoodType> RetrieveByID(int id)
		{
			try
			{
                DataProviderDataContext ctx = new DataProviderDataContext();
				return (from item in ctx.FoodTypes where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<FoodType>();
		}

	}
}